<?php
/**
 * Created by PhpStorm.
 * User: ningtaoli
 * Date: 2018/4/23
 * Time: 下午10:42
 */

namespace app\home\controller;
use app\home\controller\Buy;
use think\Controller;
use think\Db;

class Personal extends Com
{

    public function index()
    {
        $id = session('user_id');
        $User = Db::name('mumber')->where('uid',$id)->find();
        $user_msg=Db::name('mumber_team')->where('uid',$id)->find();
        $broad=Db::name('broad')->find();
        $this->assign('broad',$broad);
        $this->assign('fid',$User['fid']);
        $this->assign('user_msg',$user_msg);
         $this->assign('user',$User);
        return view();
    }

    public function zhangfu(){
        $data= Db::name('zhangfu')->limit(5)->order("id desc")->select();
        $data=$this->reverse($data);
        $data=json_encode($data,true);
        return ['row' => 1, 'val' => $data];
    }
    public function qiandao(){

        if(!session('user_id')){
            $this->redirect('Login/index');
        }
        $user=Db::name('mumber')->where('uid',session('user_id'))->find();
        $time=date("Y-m-d",time());
        $qiandao_time=date("Y-m-d",$user['qiandao_time']);
        if($qiandao_time!=$time){
            //签到操作
            Db::name('mumber')->where('uid',session('user_id'))->setInc('mymoney',0.5);
            Db::name('trade')->insert([
                'uid' => session('user_id'),
                'trade_text' => '签到',
                'trade_status' => 3,
                'uptime' => time(),
                'trade_money' =>0.5,
                'trade' => time()
            ]);
            Db::name('mumber')->where('uid',session('user_id'))->setField('qiandao_time',time());
           echo '签到成功';
        }else{
            echo '一天只能签到一次';
        }
    }
    public function k(){
        return view();
    }
    public function recharge(){
        return view();
    }
    public function  reverse($arr)
    {
        $left = 0;
        $right = count($arr) -1;
        $temp = [];
        while ($left <= $right){
            $temp[$left] = $arr[$right];
            $temp[$right]  = $arr[$left];
            $left++;
            $right--;
        }
        ksort($temp);
        return $temp;
    }

    public function com(){
        $id = session('user_id');
        $trade = Db::name('trade')
            ->where('uid',$id)
            ->where('trade_status',3)
            ->order('tid desc')
            ->paginate(20);
        $com = Db::name('trade')
            ->where('uid',$id)
            ->where('trade_status',3)
            ->sum('trade_money');
        $this->assign('com',$com);
        $this->assign('trade',$trade);
        return view();
    }

    public function tou(){
        $id = session('user_id');
        $trade = Db::name('buy')
            ->where('uid',$id)
            ->where('b_buy',1)
            ->order('bid desc')
            ->order('b_status asc')
            ->paginate(20);
        $com = Db::name('buy')
            ->where('uid',$id)
            ->where('b_buy',1)
            ->sum('price');
        $this->assign('com',$com);
        $this->assign('trade',$trade);
        return view();
    }

    //进行投资
    public function buy(){
     if(input('post.')){
       if(is_numeric(input('post.money'))){
           if(input('post.money')>20000||input('post.money')<10){
          $this->error('账户入金必须在10-20000之间');
           }
           if(input('post.type')=="alipay"){
              $buy=new Buy();
              $buy->buy(input('post.money'));
              die;
           }elseif(input('post.type')=="mymoney"){

           }else{
               $this->error('请选择支付方式');
           }
       }else{
           $this->error('非法的请求');
       }

     }
        return view();
    }

    public function tixian(){
        $id = session('user_id');
        $trade = Db::name('trade')
            ->where('uid',$id)
            ->where('trade_status',2)
            ->order('tid desc')
            ->paginate(20);
        $this->assign('trade',$trade);
        return view();
    }

    public function put(){
        $id = session('user_id');
        $trade = Db::name('put_order')
            ->where('uid',$id)
            ->order('id desc')
            ->paginate(20);
        $this->assign('trade',$trade);
        return view();
    }
    public function update_ziliao(){
        if(!session('user_id')){
            $this->redirect('Login/index');
        }
        $id = session('user_id');
        $user = Db::name('mumber')
            ->where('uid',$id)
            ->find();
        $this->assign('user',$user);
        return view();
    }
    public function jump(){
        $this->assign('data',input());
        return view();
    }
    public function update_pay(){
        if(!session('user_id')){
            $this->redirect('Login/index');
        }
        $id = session('user_id');
        $data=input('post.');

        if(!$data){
            $this->redirect('jump', ['msg'=>'请填写数据','status'=>'err']);
            die;
        }

        if(cookie('user_auth')){
            $this->redirect('jump', ['msg'=>'当天只能修改一次','status'=>'err']);
            die;
        }

     /*   $num=Db::name('control')->where('id',1)->value('update_pay_cishu');
        if(Db::name('mumber')->where('pay',$data['pay'])->count()>$num)
        {
            $this->redirect('jump', ['msg'=>'支付宝超出绑定','status'=>'err']);
            die;
        }*/
        $res = Db::name('mumber')
            ->where('uid',$id)
            ->setField(['pay'=>$data['pay'],'realname'=>$data['realname']]);
       if($res){
           cookie('user_auth',[1],3600);
           $this->redirect('jump', ['msg'=>'修改成功','status'=>'succ']);
           die;
       }else{
           $this->redirect('jump', ['msg'=>'修改失败','status'=>'err']);
           die;
       }
    }
    public function fan(){
        $id = session('user_id');

        $trade = Db::name('trade')
            ->whereor('uid',$id)
            ->where('trade_status', 'not in',[1,2,3,5])
            ->order('tid desc')
            ->paginate(20);
        $com = Db::name('trade')
            ->whereor('uid',$id)
            ->where('trade_status', 'not in',[1,2,3,5])
            ->sum('trade_money');
        $this->assign('com',$com);
        $this->assign('trade',$trade);
        return view();
    }

    public function quan(){
        $id = session('user_id');
        $trade = Db::name('trade')
            ->where('uid',$id)
            ->where('trade_status',5)
            ->order('tid desc')
            ->paginate(20);
        $this->assign('trade',$trade);
        return view();
    }

    public function pro(){
        $id = session('user_id');
        $trade = Db::name('buy')
            ->where('uid',$id)
            ->where('b_buy',1)
            ->order('bid desc')
            ->order('b_status asc')
            ->paginate(20);
        $this->assign('trade',$trade);
        return view();
    }

    public function team()
    {
          $id = session('user_id');
        $User = Db::name('mumber')->where("uid",$id)->field('acc')->find();
        //总人数
        $Ch = Db::name('mumber')->where("uid", ">" ,"{$id}")->select();
        $tree = (new Gettree())->getChTree($Ch,$id);
        $tober = count($tree);
        //团队业绩
        $money=Db::name('mumber_team')
            ->where('uid',$id)
            ->value('yeji');
        $team = (new Gettree())->getChlTree($Ch,$id);

        $this->assign('team',$team);
        $this->assign('tober',$tober);
        $this->assign('money',$money);
        return view();
    }

    public function UserOut(){
        session('user_id',null);
        session('token',null);
        $this->redirect('Login/index');
    }

    public function teams()
    {
        $id = session('user_id');
        $get = input('get.id');
        $Ch = Db::name('mumber')->where("uid", ">" ,"{$id}")->select();
        $team = (new Gettree())->getChlTree($Ch,$id);
        $this->assign('team',$team[$get]);
        return view();
    }
    
    public function tuiguang()
    {
        $fenhong1 = Db::name('mumber_team')
            ->where('tayextension', '>=', 8)
            ->count();
        $fenhong2 = Db::name('mumber_team')
            ->where('tayextension', '>=', 12)
            ->where('dangri_yeji', '>=', 3000)
            ->count();
        $fenhong3 = Db::name('mumber_team')
            ->where('leiji_zhitui', '>=', 20)
            ->where('yeji','>=',20000)
            ->count();
  /*      $fenhong4 = Db::name('mumber_team')
            ->where('leiji_zhitui', '>=', 30)
            ->where('yeji','>=',50000)
            ->count();*/
        $jiangli_time=Db::name('control')
            ->where('id',1)
            ->value('jiangli_control');
        $DayTime = $jiangli_time;
        $TomTime = time();
        //当天营业额
        $Money = Db::name('trade')
            ->where('trade_status', 1)
            ->where('uptime', '>', $DayTime)
            ->where('uptime', '<', $TomTime)
            ->sum('trade_money');
        $id = session('user_id');
        $trade = Db::name('trade')
            ->where('uid',$id)
            ->where('trade_status',5)
            ->order('tid desc')
            ->paginate(20);
        $this->assign('fenhong1',$fenhong1);
        $this->assign('fenhong2',$fenhong2);
        $this->assign('fenhong3',$fenhong3);
      /*  $this->assign('fenhong4',$fenhong4);*/
        $this->assign('trade',$trade);
        $this->assign('Money',$Money);//总收入资金池用
        return view();
    }

    public function explain(){
        return view();
    }

    public function online(){
        $res=Db::name('img')->where('id',1)->find();
        $this->assign('kefu',$res);
        return view();
    }
}